Im not sure if you still need this answered, but ill answer it for others who might see this in the future date. You cannot directly alter other class’s variable when it is set as private, so if you create an class object in the main class, and have to set the name, you can’t just do objName.name = “name”. you need to call the setName function like objName.setName(“name”);, as setName function itself is set public so anything outside of its scope can use the function freely. This is necessary when you don’t want name variable accessible freely, where many things can go wrong if you do. imagine if you have class with variables that require accurate information, but the variables are not set private and something accessed it by accident, altering it.
|